<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工" prop="partyId">
        <el-select v-model="queryParams.partyId" placeholder="请选择员工" clearable filterable>
          <el-option v-for="dict in partyIdOptions" :key="dict.partyId" :label="dict.firstName" :value="dict.partyId" />
        </el-select>
      </el-form-item>
      <el-form-item label="考勤日期">
        <el-date-picker v-model="daterangeAttendanceDate" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleDetail"
          v-hasPermi="['ehr:attend_record:edit']">详情</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ehr:attend_record:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ehr:attend_record:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ehr:attend_record:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ehr:attend_record:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attend_recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="记录ID" align="left" prop="attendanceRecordId" />
      <el-table-column label="考勤来源" align="left" prop="fromTypeId" width="80" :formatter="fromTypeIdFormat" />
      <el-table-column label="员工" align="left" prop="partyId" width="80" :formatter="partyIdFormat" />
      <el-table-column label="考勤日期" align="center" prop="attendanceDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.attendanceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考勤时间" align="center" prop="attendanceTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.attendanceTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考勤规则" align="left" prop="attendanceRuleId" width="100"
        :formatter="attendanceRuleIdFormat" />
      <el-table-column label="考勤地址" align="left" prop="attendanceAddId" width="120" :formatter="attendanceAddIdFormat" />
      <el-table-column label="纬度" align="left" prop="locationX" />
      <el-table-column label="经度" align="left" prop="locationY" />
      <el-table-column label="是否异常" align="left" prop="isNormal" :formatter="isNormalFormat" />
      <el-table-column label="最后更新时间" align="center" prop="lastUpdatedStamp" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastUpdatedStamp, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document" @click="handleDetail(scope.row)"
            v-hasPermi="['ehr:attend_record:detail']">详情</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ehr:attend_record:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ehr:attend_record:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改考勤记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" v-bind:disabled=detailFlag>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工" prop="partyId">
              <el-select v-model="form.partyId" placeholder="请选择员工">
                <el-option v-for="dict in partyIdOptions" :key="dict.partyId" :label="dict.firstName"
                  :value="dict.partyId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考勤日期" prop="attendanceDate">
              <el-date-picker clearable v-model="form.attendanceDate" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择考勤日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="考勤时间" prop="attendanceTime">
              <el-date-picker clearable v-model="form.attendanceTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                default-time="00:00:00" placeholder="请选择考勤时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考勤来源" prop="fromTypeId">
              <el-select v-model="form.fromTypeId" placeholder="请选择考勤来源">
                <el-option v-for="dict in fromTypeIdOptions" :key="dict.enumCode" :label="dict.description"
                  :value="dict.enumCode"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="考勤规则" prop="attendanceRuleId">
              <el-select v-model="form.attendanceRuleId" placeholder="请选择考勤规则">
                <el-option v-for="dict in attendanceRuleIdOptions" :key="dict.attendanceRuleId"
                  :label="dict.attendanceName" :value="dict.attendanceRuleId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考勤地址" prop="attendanceAddId">
              <el-select v-model="form.attendanceAddId" placeholder="请选择考勤地址">
                <el-option v-for="dict in attendanceAddIdOptions" :key="dict.addressId" :label="dict.addressName"
                  :value="dict.addressId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="打卡地图地址" prop="attendanceAdd">
              <el-input v-model="form.attendanceAdd" placeholder="请输入打卡地图地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="纬度" prop="locationX">
              <el-input v-model="form.locationX" placeholder="请输入纬度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经度" prop="locationY">
              <el-input v-model="form.locationY" placeholder="请输入经度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="打卡手机号" prop="mobilePhone">
              <el-input v-model="form.mobilePhone" placeholder="请输入打卡手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="APP打卡地址" prop="addressApp">
              <el-input v-model="form.addressApp" placeholder="请输入APP打卡地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否异常">
              <el-radio-group v-model="form.isNormal">
                <el-radio v-for="dict in isNormalOptions" :key="dict.enumCode" :label="dict.enumCode">{{ dict.description
                }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="editFlag" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAttend_record, getAttend_record, delAttend_record, addAttend_record, updateAttend_record } from "@/api/ehr/attend_record";

export default {
  name: "Attend_record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考勤记录表格数据
      attend_recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 考勤来源业务字典
      fromTypeIdOptions: [],
      // 员工业务字典
      partyIdOptions: [],
      // 是否打卡异常时间范围
      daterangeAttendanceDate: [],
      // 考勤规则业务字典
      attendanceRuleIdOptions: [],
      // 考勤地业务字典
      attendanceAddIdOptions: [],
      // 是否打卡异常业务字典
      isNormalOptions: [],
      // 操作标识,用于区分是新增还是修改
      updateFlag: false,
      // 操作标识,用于区分编辑状态
      editFlag: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "", // 排序字段
        isAsc: "", // 升序或降序 大写的 ASC或DESC,为空代表升序
        partyId: null,
        attendanceDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    // 考勤来源业务字典
    this.getEnumDicts("ATTEND_SOURCE").then((response) => {
      this.fromTypeIdOptions = response.data;
    });
    // 员工业务字典
    this.getEntityDicts("Person:partyId:firstName").then((response) => {
      this.partyIdOptions = response.data;
    });
    // 考勤规则业务字典
    this.getEntityDicts("AttendanceRule:attendanceRuleId:attendanceName").then((response) => {
      this.attendanceRuleIdOptions = response.data;
    });
    // 考勤地业务字典
    this.getEntityDicts("AttendanceAddress:addressId:addressName").then((response) => {
      this.attendanceAddIdOptions = response.data;
    });
    // 是否打卡异常业务字典
    this.getEnumDicts("YES_NO").then((response) => {
      this.isNormalOptions = response.data;
    });
    this.getList();
  },
  methods: {
    fromTypeIdFormat(row, column) {
      let val = null;
      this.fromTypeIdOptions.forEach((item, index) => {
        if (row.fromTypeId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    partyIdFormat(row, column) {
      let val = null;
      this.partyIdOptions.forEach((item, index) => {
        if (row.partyId === item.partyId) {
          val = item.firstName;
        }
      });
      return val;
    },
    attendanceRuleIdFormat(row, column) {
      let val = null;
      this.attendanceRuleIdOptions.forEach((item, index) => {
        if (row.attendanceRuleId === item.attendanceRuleId) {
          val = item.attendanceName;
        }
      });
      return val;
    },
    attendanceAddIdFormat(row, column) {
      let val = null;
      this.attendanceAddIdOptions.forEach((item, index) => {
        if (row.attendanceAddId === item.addressId) {
          val = item.addressName;
        }
      });
      return val;
    },
    isNormalFormat(row, column) {
      let val = null;
      this.isNormalOptions.forEach((item, index) => {
        if (row.isNormal === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    /** 查询考勤记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeAttendanceDate && '' != this.daterangeAttendanceDate) {
        this.queryParams.params["beginAttendanceDate"] = this.daterangeAttendanceDate[0];
        this.queryParams.params["endAttendanceDate"] = this.daterangeAttendanceDate[1];
      }
      listAttend_record(this.queryParams).then(response => {
        this.attend_recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        attendanceRecordId: null,
        fromTypeId: null,
        partyId: null,
        attendanceDate: null,
        attendanceTime: null,
        attendanceRuleId: null,
        attendanceAddId: null,
        attendanceAdd: null,
        locationX: null,
        locationY: null,
        mobilePhone: null,
        addressApp: null,
        remark: null,
        client: null,
        jobReport: null,
        isNormal: "0",
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeAttendanceDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.attendanceRecordId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.updateFlag = false;
      this.reset();
      this.open = true;
      this.title = "添加考勤记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.updateFlag = true;
      this.reset();
      const attendanceRecordId = row.attendanceRecordId || this.ids
      getAttend_record(attendanceRecordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考勤记录";
      });
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.editFlag = false,
        this.updateFlag = false;
      this.detailFlag = true;
      this.reset();
      const attendanceRecordId = row.attendanceRecordId || this.ids
      getAttend_record(attendanceRecordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "考勤记录详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.attendanceRecordId != null && this.updateFlag) {
            updateAttend_record(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttend_record(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const attendanceRecordIds = row.attendanceRecordId || this.ids;
      this.$modal.confirm('是否确认删除考勤记录编号为"' + attendanceRecordIds + '"的数据项？').then(function () {
        return delAttend_record(attendanceRecordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehr/attend_record/export', {
        ...this.queryParams
      }, `attend_record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
